﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using visinia.SmartEngine;
using visinia.SmartEngine.Installer;
using visinia.SmartEngine.Modules;
using visinia.SmartEngine.Pages;

public partial class Admin_Controls_ToolBox : System.Web.UI.UserControl
{
    // an orphan = if a pane in theme is renamed or deleted which has modules in it those modules 
    // are called orphan modules, and they will not show up on the page
    //so those modules will be displayed here.
    public List<ModuleInfo> OrphanModules = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        LiteralControl toolBox = new LiteralControl(Render());
        Controls.Add(toolBox);
    }

    private string Render()
    {
        int currentPageId = 0;
        string currentPackage = null;
        Int32.TryParse(Request.QueryString["PageId"], out currentPageId);
        if (currentPageId > 0)
        {
            PageInfo currentPage = PageController.GetPage(currentPageId);
            if(currentPage != null)
            {
                Plugin plugin = PluginController.GetInstalledPlugin(currentPage.PluginId);
                currentPackage = plugin.Name;
            }
        }

        List<ModuleInstalled> modules = ModulesInstalledController.GetInstalledModules();
        string root = WebRoots.GetModulesRoot;
        string dummy = WebRoots.GetAdminRoot + "Themes/_Common/images/blank.gif";        
        StringBuilder sb = new StringBuilder();
        sb.Append("<ul class=\"nav\"><li class=\"nav-container\"><a class=\"nav-header\" >Toolbox</a>");
        sb.Append("<ul class=\"nav-main\">");
            foreach(string package in GetPackages())
            {
                if(string.Compare(currentPackage, package, true) == 0)
                    sb.Append("<li><a class=\"current-page\">" + package + "</a>");
                else
                    sb.Append("<li><a>" + package + "</a>");
                        sb.Append("<ul class=\"nav-sub tool-box\" >");
                        foreach (ModuleInstalled module in modules)
                        {
                            if (module.Package == package)
                            {
                                sb.Append("<li>");
                                    sb.Append("<div id=\"" + module.ModuleInstalledId + "\" class=\"new-module\" >");
                                    if (string.Compare(currentPackage, package, true) == 0)
                                        sb.Append("<a class=\"current-page\">");
                                    else
                                        sb.Append("<a>");
                                            if (string.IsNullOrEmpty(module.Icon))
                                                sb.Append("<img src=\"" + dummy + "\" alt=\"" + module.Name + "\" width=\"12\" height=\"12\" />");
                                            else
                                                sb.Append("<img src=\"" + root + module.Icon + "\" alt=\"" + module.Name + "\" width=\"12\" height=\"12\" />");
                                            sb.Append(module.Name);
                                        sb.Append("</a>");
                                    sb.Append("</div>");
                                sb.Append("</li>");
                            }
                        }
                        sb.Append("</ul>");
                sb.Append("</li>");
            }
            if (OrphanModules.Count > 0)
            {
                // orphan modules
                sb.Append("<li><a>Orphan Modules</a>");
                 sb.Append("<ul class=\"nav-sub tool-box\" >");
                        OrphanModules.ForEach(delegate(ModuleInfo module)
                        {
                            ModuleInstalled moduleInstalled = ModulesInstalledController.GetInstalledModule(module.ModuleInstalledId);
                            sb.Append("<li>");
                                sb.Append("<div id=\"" + module.ModuleId + "\" class=\"orphan-module dragbox\" >");
                                    sb.Append("<a>");
                                        if (string.IsNullOrEmpty(moduleInstalled.Icon))
                                            sb.Append("<img src=\"" + dummy + "\" alt=\"" + module.Title + "\" width=\"16\" height=\"16\" />");
                                        else
                                            sb.Append("<img src=\"" + root + moduleInstalled.Icon + "\" alt=\"" + module.Title + "\" width=\"16\" height=\"16\" />");                                      
                                        sb.Append(moduleInstalled.Name + "[" + module.Pane + "]");
                                    sb.Append("</a>");
                                sb.Append("</div>");
                            sb.Append("</li>");                    
                        });
                    sb.Append("</ul>");
                sb.Append("</li>");
            }
            sb.Append("</ul>");
        sb.Append("</li></ul>");
        return sb.ToString();
    }    

    private List<string> GetPackages()
    {
        List<ModuleInstalled> modules = ModulesInstalledController.GetInstalledModules();
        List<string> DistinctPackages = new List<string>();
        foreach (ModuleInstalled module in modules)
        {
            if (!DistinctPackages.Exists(p => p == module.Package))
                DistinctPackages.Add(module.Package);
        }
        return DistinctPackages;
    }
}
